<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use DB;

class WarehouseOutboundDetail extends Model
{
    protected $table = 'warehouse_outbound_detail';
    public function getorders($id){
        $count = 11-mb_strlen($id);
        return 'RK'.mt_rand(0,$count).$id;
    }
    //单位名称
    public function warehouseMaterials()
    {
        return $this->hasMany('App\Model\WarehouseMaterials','order_id');
    }
    //所属单位分类
    public function stock()
    {
        return $this->belongsTo('App\Model\Stock','stock_id');
    }
    //所属单位分类
    public function warehouseOutbound()
    {
        return $this->belongsTo('App\Model\WarehouseOutbound','outbound_id');
    }

    public static function getOutBoundNum($goods_id=0,$material_id=0,$checkcategory_id=0,$company_id=0,$classify=0){
        $stock = self::with([
            'stock' => function ($q) use ($goods_id,$material_id,$checkcategory_id,$company_id) {
                $q->where('goods_id',$goods_id)->where('material_id',$material_id)->where('checkcategory_id',$checkcategory_id)->where('company_id',$company_id);
            }])->where('outbound_type',$classify)->get()->toArray();
        foreach ($stock as $key=>$val){
            if(empty($val['stock'])){
                unset($stock[$key]);
            }
        }
        $stock_ids = array_column($stock,'stock_id');
        $outboundNum_all = self::select(DB::raw('SUM(`number`) as sumnum'))->whereIn('stock_id',$stock_ids)->first();
        if($classify==3){ //报废出库
            $outboundNum = self::select(DB::raw('SUM(`number`) as sumnum'))->whereIn('stock_id',$stock_ids)->where('outbound_type',$classify)->first();
        }else{
            $outboundNum = self::select(DB::raw('SUM(`number`) as sumnum'))->whereIn('stock_id',$stock_ids)->whereIn('outbound_type',[1,2])->first();
        }

        return ['outboundNum_all'=>$outboundNum_all['sumnum']??0,'outboundNum'=>$outboundNum['sumnum']??0];
    }

    /**
     * 物资名称下的出库单
     * @param $material_id
     * @param $classify
     * @return array
     */
    public static function getOutMaterialBymid($material_id=0,$classify=[]){
        $stock = self::with([
            'warehouseOutbound',
            'warehouseOutbound.users:id,name',
            'stock' => function ($q) use ($material_id) {
                $q->select('id');
                $q->where('material_id',$material_id);
            }])->whereIn('outbound_type',$classify)->get()->toArray();
        foreach ($stock as $key=>$val){
            if(empty($val['stock'])){
                unset($stock[$key]);
            }
        }
        return $stock;
    }

    /**
     * 物资名称下的出库单
     * @param $material_id
     * @param $classify
     * @return array
     */
    public static function getOutMaterialBysid($stock_id=0,$classify=[]){
        $stock = self::with([
            'warehouseOutbound',
            'warehouseOutbound.users:id,name',
           ])->where('stock_id',$stock_id)->whereIn('outbound_type',$classify)->get()->toArray();
        return $stock;
    }
}
